# coding: utf8
'''扩展模型models'''

# 模型
from it.models import Tasks

# 配置
from it.config import *

# 工具箱
from common.utils import time as th

from django.utils.html import format_html

class ExtTasks(Tasks):
	class Meta:
		proxy = True

	def get_status(self):
		'''状态码转为中文状态'''
		full_status = ''

		try:
			full_status = config['task']['status'][self.status]
		except:
			full_status = '状态不存在'  # 避免config或者tasks表被不小心改动带来体验度差问题
		finally:
			return full_status

	def get_add_time(self):
		'''可读添加时间,月-日 时:分'''
		return th.mysql_time_to_datatime(self.add_time, True)

	def get_expect_time(self):
		'''可读的期望解决时间, 提交时间 到 期望时间'''
		expect_time = th.mysql_time_to_datatime(self.expect_time, True)
		expect_total_time = th.time_differ(self.add_time, self.expect_time)
		# return '%s, %s 内解决' % (expect_time, expect_total_time)
		return '%s 内解决, %s' % (self.expect_time_comment, expect_time)

	def get_accept_time(self):
		'''可读的受理时间, 提交时间 到 受理时间'''
		if self.accept_time:
			accept_time = th.mysql_time_to_datatime(self.accept_time, True)
			wait_time = th.time_differ(self.add_time, self.accept_time)
			return '%s 后被受理, %s' % (wait_time, accept_time)
		return '-'

	def get_resolved_time(self):
		'''可读的解决时间, 受理时间 到 解决时间'''
		if self.resolved_time:
			resolved_time = th.mysql_time_to_datatime(self.resolved_time, True)
			used_time = th.time_differ(self.accept_time, self.resolved_time)
			return '用时 %s, %s' % (used_time, resolved_time)
		return '-'

	def is_exceed(self):
		'''是否超期完成'''
		# 解决时间大于期望时间
		if self.resolved_time:
			return self.resolved_time > self.expect_time
		return False

	def get_total_time(self):
		'''总时长, 提出时间 到 解决时间'''
		if self.resolved_time:
			total_time = th.time_differ(self.add_time, self.resolved_time)
			if self.is_exceed():
				# 延期
				msg = '超时 %s 解决' % th.time_differ(self.expect_time, self.resolved_time)
			else:
				# 如期
				msg = '如期解决'
			return '%s, %s' % (total_time, msg)
		return '-'

	def how_long(self):
		'''距离添加时间,过去多久了'''
		return th.time_span(self.add_time)
